/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xiaofei.library.hermestest.slice;

import com.xiaofei.library.hermes.Hermes;
import com.xiaofei.library.hermes.bean.BaseBean;
import com.xiaofei.library.hermes.stub.StubCallback;
import com.xiaofei.library.hermestest.ResourceTable;
import com.xiaofei.library.hermestest.Utils;
import com.xiaofei.library.hermestest.service.BaseBeanServiceAbility;
import com.xiaofei.library.hermestest.service.BooleanServiceAbility;
import com.xiaofei.library.hermestest.service.ByteServiceAbility;
import com.xiaofei.library.hermestest.service.DoubleServiceAbility;
import com.xiaofei.library.hermestest.service.FloatServiceAbility;
import com.xiaofei.library.hermestest.service.IntServiceAbility;
import com.xiaofei.library.hermestest.service.LongServiceAbility;
import com.xiaofei.library.hermestest.service.ShortServiceAbility;
import com.xiaofei.library.hermestest.service.StringServiceAbility;
import com.xiaofei.library.hermestest.service.VoidServiceAbility;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

/**
 * 测试功能界面
 *
 * @author:Hexy
 * @since 2021-04-14
 */
public class TestAbilitySlice extends AbilitySlice {
    private static final int NUMBER = 2;
    private static final int TEST = 1000;
    private static final int RANDOM_C = 10;
    private TestAbilitySlice mTestAbilitySlice;

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_test);
        mTestAbilitySlice = this;
        setListener();
        clickListener();
        clickListener1();
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 点击监听函数
     */
    public void setListener() {
        findComponentById(ResourceTable.Id_btn_test_void).setClickedListener(component -> {
            Hermes.handleVoid(VoidServiceAbility.class.getName(), this,
                    (StubCallback<String>) value1 -> Utils.showDialog(getContext(), "handleVoid: " + value1));
        });
        findComponentById(ResourceTable.Id_btn_test_boolean).setClickedListener(component -> {
            Hermes.handleBoolean(BooleanServiceAbility.class.getName(), this,
                    false, false,
                    (StubCallback<String>) value1 -> Utils.showDialog(getContext(), "handleBoolean: " + value1));
        });
        findComponentById(ResourceTable.Id_btn_test_byte).setClickedListener(component -> {
            byte[] targets = new byte[NUMBER];
            Hermes.handleByte(ByteServiceAbility.class.getName(), this,
                    targets[0], targets[0], (StubCallback<String>)
                            value1 -> Utils.showDialog(getContext(), "handleByte: " + value1));
        });
        findComponentById(ResourceTable.Id_btn_test_short).setClickedListener(component -> {
            Hermes.handleShort(ShortServiceAbility.class.getName(), mTestAbilitySlice,
                    (short) 1, (short) 1, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleShort: " + value);
                    });
        });
        findComponentById(ResourceTable.Id_btn_test_int).setClickedListener(component -> {
            Hermes.handleInt(IntServiceAbility.class.getName(), mTestAbilitySlice,
                    RANDOM_C, RANDOM_C, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleInt: " + value);
                    });
            Hermes.handleInt(IntServiceAbility.class.getName(), mTestAbilitySlice,
                    RANDOM_C, RANDOM_C, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleInt: " + value);
                    });
        });
        findComponentById(ResourceTable.Id_btn_test_long).setClickedListener(component -> {
            Hermes.handleLong(LongServiceAbility.class.getName(), mTestAbilitySlice,
                    RANDOM_C, RANDOM_C, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleLong: " + value);
                    });
        });
        findComponentById(ResourceTable.Id_btn_test_float).setClickedListener(component -> {
            Hermes.handleFloat(FloatServiceAbility.class.getName(), mTestAbilitySlice,
                    RANDOM_C, RANDOM_C, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleFloat: " + value);
                    });
        });
    }

    /**
     * 点击监听函数
     */
    public void clickListener() {
        findComponentById(ResourceTable.Id_btn_test_double).setClickedListener(component -> {
            Hermes.handleDouble(DoubleServiceAbility.class.getName(), mTestAbilitySlice,
                    RANDOM_C, RANDOM_C, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleDouble: " + value);
                    });
            Hermes.handleDouble(DoubleServiceAbility.class.getName(), mTestAbilitySlice,
                    RANDOM_C, RANDOM_C, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleDouble: " + value);
                    });
        });

        findComponentById(ResourceTable.Id_btn_test_string).setClickedListener(component -> {
            Hermes.handleString(StringServiceAbility.class.getName(), mTestAbilitySlice,
                    "01", "20", (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleString: " + value);
                    });
        });

        findComponentById(ResourceTable.Id_btn_test_bean).setClickedListener(component -> {
            BaseBean baseBean = new BaseBean();
            baseBean.setVal1(1);
            baseBean.setVal2(true);
            baseBean.setVal3("3");
            Hermes.handleBean(BaseBeanServiceAbility.class.getName(), mTestAbilitySlice,
                    baseBean, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleString: " + value);
                    });
        });

        findComponentById(ResourceTable.Id_btn_test_callback).setClickedListener(component -> {
            Hermes.handleInt(IntServiceAbility.class.getName(), mTestAbilitySlice, RANDOM_C,
                    RANDOM_C, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleCallback: " + value);
                    });
            Hermes.handleInt(IntServiceAbility.class.getName(), mTestAbilitySlice, RANDOM_C,
                    RANDOM_C, (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), "handleCallback: " + value);
                    });
        });
    }

    /**
     * 点击监听函数
     */
    public void clickListener1() {
        findComponentById(ResourceTable.Id_btn_test_null_test).setClickedListener(component -> {
            String str = "handleNullTest: ";
            Hermes.handleLong(LongServiceAbility.class.getName(), mTestAbilitySlice, RANDOM_C, RANDOM_C,
                    (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), str + value);
                    });
            Hermes.handleLong(LongServiceAbility.class.getName(), mTestAbilitySlice, RANDOM_C, RANDOM_C,
                    (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), str + value);
                    });
            Hermes.handleLong(LongServiceAbility.class.getName(), mTestAbilitySlice, 0, RANDOM_C,
                    (StubCallback<String>) value -> {
                        Utils.showDialog(getContext(), str + value);
                    });
        });

        findComponentById(ResourceTable.Id_btn_test_gc).setClickedListener(component -> {
            Utils.showDialog(getContext(), "开始进行压力测试");
            new Thread(() -> {
                for (int num = 0; num < TEST; num++) {
                    Hermes.handleInt(IntServiceAbility.class.getName(), mTestAbilitySlice, 0,
                            0, (StubCallback<String>) value -> {
                            });
                }
            }).start();
        });
        findComponentById(ResourceTable.Id_btn_test_callback_gc).setClickedListener(component -> {
            Utils.showDialog(getContext(), "开始进行压力测试");
            new Thread(() -> {
                for (int num = 0; num < TEST; num++) {
                    Hermes.handleInt(IntServiceAbility.class.getName(), mTestAbilitySlice, 0,
                            0, null);
                }
            }).start();
        });
    }
}
